//
//  AppDelegate.m
//  0.3camera
//
//  Created by 茶 on 2023/11/23.
//

#import "AppDelegate.h"
#import "CANavController.h"
#import "CAADManager.h"
#import "CAIAPTool.h"

#import <UMCommon/UMConfigure.h>
#import <XYIAPKit/XYIAPKit.h>
#import <XYIAPKit/XYStoreUserDefaultsPersistence.h>
#import <XYIAPKit/XYStoreiTunesReceiptVerifier.h>

static NSString *const sharePass = @"527e70dfe17b4492b39999a7ca0a3504";

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self initApplication];
    return YES;
}



- (void)initApplication {
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [self initIAPTool]; //内购
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = UIColor.whiteColor;
    
    
#ifdef DEBUG
    CANavController *nav = [[CANavController alloc] initWithRootViewController:[NSClassFromString(@"HomeViewController") new]];
    self.window.rootViewController = nav;
#else
    if (CAUserBool(kUserAgree)) {
        UIViewController *launchVC = [UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil].instantiateInitialViewController;
        self.window.rootViewController = launchVC;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (![CAADManager sharedInstance].isOpeningShow) {
                [self adIsClose];
            }
        });
    } else {
        CANavController *nav = [[CANavController alloc] initWithRootViewController:[NSClassFromString(@"HomeViewController") new]];
        self.window.rootViewController = nav;
        [kUserDefaults setBool:YES forKey:kUserAgree];
    }
#endif
    
    [self.window makeKeyAndVisible];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        CAWeakify(self);
        [UIDevice ca_idfaWithResult:^(NSString * _Nonnull idfa) {
            dispatch_async(dispatch_get_main_queue(), ^{
                #ifdef DEBUG

                #else
                    CAStrongify(self);
                    [self initAd];
                    [self initUM];
                #endif
                
            });
        }];
    });
}

- (void)initUM{
    [UMConfigure initWithAppkey:@"655ebc9db2f6fa00ba84d119" channel:@"App Store"];
}

- (void)initAd {
    CAWeakify(self);
    [[CAADManager sharedInstance] initADManager];
    [CAADManager sharedInstance].openClose = ^{
        CAStrongify(self);
        [self adIsClose];
    };
}

- (void)adIsClose{
    if (![self.window.rootViewController isKindOfClass:CANavController.class]) {
        CANavController *nav = [[CANavController alloc] initWithRootViewController:[NSClassFromString(@"HomeViewController") new]];
        self.window.rootViewController = nav;
    }
}

#pragma mark --✂️-- 内购工具
- (void)initIAPTool {
    // 验证票据
    [XYStoreiTunesReceiptVerifier shareInstance].sharedSecretKey = sharePass;
    [[XYStore defaultStore] registerReceiptVerifier:[XYStoreiTunesReceiptVerifier shareInstance]];
    [[XYStore defaultStore] registerTransactionPersistor:[XYStoreUserDefaultsPersistence shareInstance]];
    [[CAIAPTool shareTool] ca_requestAllProductWithCompletion:nil];
}

@end
